<?php

class GiftsController extends AppController {


/**
 * Uses array
 *
 * @var array
 */
    public $uses = array('Gift');

/**
 * Helpers
 *
 * @var array
 **/
  public $components = array(
                        'RequestHandler',
                        'CakePdf.CakePdf' => array(
                            'prefix' => 'mypdf',
                            'orientation' => 'L'
                        )
                      );

  public $helpers = array('Html', 'Form', 'Paginator', 'Js' => array('Jquery'), 'Number');

/**
 * Pagination
 *
 * @var array
 **/
  public $paginate = array(
        'order' => array('id' => 'asc'),
        'limit' => 15
  );
    
  public function beforeFilter() {
      parent::beforeFilter();
  }

/**
 * @method: index
 * @purpose: list of all tickets
 * @param none
 * @return void
 **/
  public function admin_index() {
      $this->set('gifts', $this->paginate('Gift'));
  }
    
/**
 * @method: add 
 * @purpose: admin to add template
 * @param none
 * @return void
 **/
  public function admin_add() {
      if ($this->request->is('post')) {    
          $this->request->data['Gift']['value']=0;
          if ($this->Gift->save($this->request->data)) {
              $this->Session->setFlash('Gift Card has been saved.');
              $this->redirect(array('action' => 'index'));
          } else {
              $this->Session->setFlash('Could not save Gift Card, try later.');
          }
      }
	}
    
/**
 * @method: detail
 * @purpose: detail of enquiry ticket
 * @param none
 * @return void
 **/
  public function admin_view($id = null) {
        if (!$id) {
                $this->Session->setFlash(__('Invalid Gift Card', true));
                $this->redirect(array('action' => 'index'));
        }
        $gift = $this->Gift->read(null, $id);
        $this->Zend->writeGiftCode(WWW_ROOT.'/files/vouchers/'.$gift['Gift']['code'].'.png',$gift['Gift']['code']);
        $this->set('gift', $gift );
  }
  
  public function mypdf_print($id = null) {
      $this->CakePdf->addStyle('pdf_style.css');
      $gift = $this->Gift->read(null, $id);
      $this->CakePdf->setFilename('GiftCard_'.$gift['Gift']['code']);
        $this->Zend->writeGiftCode(WWW_ROOT.'/files/vouchers/'.$gift['Gift']['code'].'.png',$gift['Gift']['code']);
        $this->set('gift', $gift);
  }


/**
 * @method: edit
 * @purpose: edit gift card
 * @param none
 * @return void
 **/
  public function admin_edit($id = null){
  		if (!$id && empty($this->request->data)) {
  			$this->Session->setFlash(__('Invalid Gift Card', true));
  			$this->redirect(array('action' => 'index'));
  		}
  		if ($this->request->is('post') || $this->request->is('put')) { 
  			if ($this->Gift->save($this->request->data)) {
  				$this->Session->setFlash(__('The Gift Card has been saved', true));
  				$this->redirect(array('action' => 'index'));
  			} else {
  				$this->Session->setFlash(__('The Gift Card could not be saved. Please, try again.', true));
  			}
  		}
  		if (empty($this->request->data)) {
  			$this->request->data = $this->Gift->read(null, $id);
  		}
  }
  
  
/**
 * @method: admin_delete
 * @purpose: admin to delete template
 * @param none
 * @return void
 **/
	function admin_delete($id = null) {
  		if (!$id) {
  			$this->Session->setFlash(__('Invalid id for Gift Card', true));
  			$this->redirect(array('action'=>'index'));
  		}
  		if ($this->Gift->delete($id)) {
  			$this->Session->setFlash(__('Gift Card deleted', true));
  			$this->redirect(array('action'=>'index'));
  		}
  		$this->Session->setFlash(__('Gift Card was not deleted', true));
  		$this->redirect(array('action' => 'index'));
	}
       public function generate_code() {
            $this->autoRender=false;
            return $this->Emstraining->loyalty_gift_number();
        }
        
       public function admin_generate_code() {
            $this->autoRender=false;
            return $this->Emstraining->loyalty_gift_number();
        }
  
  
}